<?php
namespace Admin\Controller;
use Think\Controller;
class NoticeController extends BaseController {
    public function index(){
		$p=$_GET['p']?$_GET['p']:1;
		$psize=C('BB_PSIZE')?C('BB_PSIZE'):20;
		if(I('type')){
			$map['type']=I('type');
		}
		
		
		$notice = M('notice');
		$cache = $notice->where($map)->page($p,$psize)->order('id asc')->select();
		
		

		$count=$notice->where($map)->count();
		$this->getPage($count, $psize, 'bb-loader', '留言中心','bb-search');
		
		
		$user = M('weixin_client_user');
		$user1 = M('client_user');
		
		foreach($cache as $k => $v){
			$list=$user->where('openid = "'.$v['openid'].'"')->find();
			$cache[$k]['headimgurl']=$list['headimgurl'];
			$list1=$user1->where('openid = "'.$v['openid'].'"')->find();
			$cache[$k]['username']=$list1['username'];
			$cache[$k]['sex']=$list1['sex'];
			$cache[$k]['phone']=$list1['phone'];
		}
		
		
		
		
		$this->assign('cache',$cache);
		
		$notice1 = M('notice_category');
		$cache1 = $notice1->select();
		$this->assign('cache1',$cache1);
		
		$this->display();
    }
	
	public function category(){
		$notice = M('notice_category');
		$cache = $notice->select();
		$this->assign('cache',$cache);
		$this->display();
	}
	
	public function set(){
		$m=M('notice_category');
		$id=I('id');
		$cache=$m->find($id);
		$this->assign('cache',$cache);
		$this->display();
	}
	
	public function del(){
		$m=M('notice_category');
		$id=I('id');
		if($id){
			$re=$m->delete($id);
			if($re){
				$info['status']=1;
				$info['msg']='删除成功！';
			}else{
				$info['status']=0;
				$info['msg']='删除失败！';
			}
		}else{
			$info['status']=0;
			$info['msg']='删除失败！';
		}
		$this->ajaxreturn($info);
	}
	
	public function do_set(){
		if(!$_POST['id']){
			$data['name']=$_POST['title'];
			$data['status']=$_POST['status'];
			$notice = M('notice_category');
			$cache = $notice->add($data);
			if($cache){
				$info['status']=1;
						$info['msg']='添加成功！';
			}else{
				$info['status']=0;
						$info['msg']='添加失败！';
			}
			$this->ajaxreturn($info);
		}else{
			$data['name']=$_POST['title'];
			$data['status']=$_POST['status'];
			$notice = M('notice_category');
			$cache = $notice->where('id = '.$_POST['id'])->save($data);
			if($cache){
				$info['status']=1;
						$info['msg']='添加成功！';
			}else{
				$info['status']=0;
						$info['msg']='添加失败！';
			}
			$this->ajaxreturn($info);
		}
		
		
		
	}
	
	public function n_del(){
		$m=M('notice');
		$id=I('id');
		if($id){
			$re=$m->delete($id);
			if($re){
				$info['status']=1;
				$info['msg']='删除成功！';
			}else{
				$info['status']=0;
				$info['msg']='删除失败！';
			}
		}else{
			$info['status']=0;
			$info['msg']='删除失败！';
		}
		$this->ajaxreturn($info);
	}
	
	
	
}